/* 公用方法 */
import config from "@/app.config.js";
import $util from './util.js'
import $api from './api.js'

let $GLOBAL;

export default {
	// 初始化数据
	initData(globalData) {
		$GLOBAL = globalData;
		this.getCity();
		this.getAllDict();
	},
	getUserInfo(){
		$util.request($api.GetUserInfo)
			.then(res => {
				// console.log('取用户 res', res);
				getApp().globalData.userInfo = res.user;
				vk.setVuex('$user.userInfo', res.user);
			})
	},
	// 获取城市树
	getCity(){
		vk.callFunction({
			url: 'client/sakj/pub.lhfj.getCityTree'
		})
		.then(res => {
			$GLOBAL.cityOptions = res.data;
		})
	},
	// 全部字典项
	async getAllDict() {
		await this.getDicts('score_guanlimoshi',   'score_guanlimoshi'  );
		await this.getDicts('score_guanlituandui', 'score_guanlituandui' );
		await this.getDicts('score_danweixingzhi', 'score_danweixingzhi' );
		await this.getDicts('score_jingyingmoshi', 'score_jingyingmoshi' );
		await this.getDicts('score_have_not',      'score_have_not'      );
		await this.getDicts('score_tijian',        'score_tijian'        );
		await this.getDicts('score_peixun',        'score_peixun'        );
		await this.getDicts('score_sebiao',        'score_sebiao'        );
		await this.getDicts('score_suyuan',        'score_suyuan'        );
		await this.getDicts('score_degree',        'score_degree'        );
		
		uni.$emit('dict-init')
	},
	// 获取字典项
	getDicts(dictType, optionsName){
		// // 调云函数写法
		// vk.callFunction({
		// 	url: 'client/sakj/pub.lhfj.getDicts',
		// 	data: {dictType}
		// })
		// .then(res => {
		// 	$GLOBAL[optionsName] = res.data;
		// })
		
		// 调java接口写法
		$util.request($api.GetDicts + dictType)
			.then(res => {
				$GLOBAL[optionsName] = res.data;
			})
			.catch(err => {console.log('查询字典数据信息失败！', err);})
	},
}
